using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

public class NetManager : MonoSingleton<NetManager>
{
    public event Action<NetPackage> OnRecvNetPakage;

    private TcpConnect tcpConnect = new TcpConnect();

    private void Awake()
    {
        tcpConnect.OnHandleRecvMessage += OnHandleRecvMessage;
    }

    private void Update()
    {
        tcpConnect.UpdateRecvPkgs();
    }

    private void OnDestroy()
    {
        tcpConnect.OnHandleRecvMessage -= OnHandleRecvMessage;
        tcpConnect.Close();
    }

    public void Connect(string hostNameOrAddress, int port)
    {
        tcpConnect.Connect(hostNameOrAddress, port);
    }

    public void Send(byte[] data)
    {
        tcpConnect.Send(data);
    }

    private void OnHandleRecvMessage(NetPackage npkg)
    {
        OnRecvNetPakage?.Invoke(npkg);
    }
}
